package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xgkj.diyiketang.activity.data.JiLuPianAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.MoreVideoListTask;
import com.xgkj.diyiketang.util.EmptyDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/5/23.
 */
public class Fragment_JiLuPian extends BaseFragment implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
    private Context mContext;
    private PullToRefreshListView mListView;
    private JiLuPianAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int jlpTaskId = -1;
    private LinearLayout mEmptyView;
    private LinearLayout mEmptyContentView;

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jilupian,null);
        mContext = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view){
        mEmptyView = (LinearLayout) view.findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) view.findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        mListView = (PullToRefreshListView)view.findViewById(R.id.jilupian_list);
        mListView.init(mContext,true);
        mAdapter = new JiLuPianAdapter(mContext);
        mListView.setAdapter(mAdapter,true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
    }

    private void getJiluPian() {
        if(jlpTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        MoreVideoListTask task = new MoreVideoListTask(size, currentPage,"1",null,null,null);
        task.addListener(new JiLuPianListener(mContext));
        jlpTaskId = getEatShowApp().getTaskManager().addTask(task);
    }


    class JiLuPianListener extends ShowToastTaskEventListener {
        public JiLuPianListener(Context context) {
            super(context);
        }
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == jlpTaskId){
                jlpTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<VideoInfo> fansList = new ArrayList<VideoInfo>();
                if(data != null){
                    fansList = (List<VideoInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                currentPage = result;
                if(mAdapter != null && mAdapter.getCount() == 0){
                    EmptyDataUtil.updateWenziEmptyContentView(mEmptyView, mEmptyContentView, mListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentView(mEmptyView, mEmptyContentView, mListView);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == jlpTaskId){
                jlpTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
                mListView.doRefresh();
            }

        }
    }
    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getJiluPian();
    }

    @Override
    public void onRefresh() {
        currentPage=1;
        getJiluPian();

    }


}
